package com.ruoyi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.domain.entity.AtricleModel;
import com.ruoyi.admin.domain.vo.request.atricle.AtriclePageJoinRequest;
import com.ruoyi.admin.domain.vo.response.atricle.AtriclePageJoinResponse;
import com.ruoyi.admin.domain.vo.response.atricle.AtriclePageResponse;
import com.ruoyi.admin.domain.vo.response.atricle.AtricleResponse;
import com.ruoyi.admin.mapper.AtricleMapper;
import com.ruoyi.admin.service.AtricleService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.api.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 货号表(Atricle)Service实现类
 *
 * @author jianhan
 * @date 2023-01-31 10:49:28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AtricleServiceImpl extends ServiceImpl<AtricleMapper, AtricleModel> implements AtricleService {

    private static final Logger logger = LoggerFactory.getLogger(AtricleServiceImpl.class);

    @Override
    public Result<AtricleResponse> getId(Long atricleId) {
        AtricleModel atricleModel = this.getById(atricleId);
        if (atricleModel != null) {
            AtricleResponse atricleResponse = new AtricleResponse(atricleModel);
            return Result.success(atricleResponse);
        }
        return Result.failed(ResultCode.DATA_NOT_EXIST);
    }

    @Override
    public Result<AtriclePageResponse> page(Page<AtriclePageJoinRequest> page, AtriclePageJoinRequest atriclePageJoinRequest) {

        Page<AtriclePageJoinResponse> pages = baseMapper.queryPage(page, atriclePageJoinRequest);
        AtriclePageResponse atriclePageJoinResponse = new AtriclePageResponse(pages);
        return Result.success(atriclePageJoinResponse);
    }

    @Override
    public Result<List<AtriclePageJoinResponse>> selectList(AtriclePageJoinRequest atriclePageJoinRequest) {
        List<AtriclePageJoinResponse> atriclePageJoinResponses = baseMapper.selectListA(atriclePageJoinRequest);
        return Result.success(atriclePageJoinResponses);
    }

    @Override
    public Result add(AtricleModel atricleModel) {
        List<String> collect = Arrays.asList(atricleModel.getSize().split(",")).stream().collect(Collectors.toList());
        List<AtricleModel> atricleModels = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        for (String s : collect) {
            AtricleModel temp = new AtricleModel();
            BeanUtils.copyProperties(atricleModel, temp);
            temp.setSize(s);
            List<AtricleModel> atricleModels1 = baseMapper.selectList(new QueryWrapper<AtricleModel>().eq("article_number", temp.getArticleNumber()).eq("brand_id", temp.getBrandId()).eq("size", temp.getSize()));
            if (atricleModels1.size() > 0) {
                sb.append("码数：" + temp.getSize() + "已存在");
                continue;
            }
            atricleModels.add(temp);
        }
        if (atricleModels.size() > 0) {
            Boolean result = this.saveBatch(atricleModels);
            if (result) {
                return Result.success(sb);
            }
            return Result.failed("添加失败");
        } else {
            return Result.failed("重复添加");
        }
    }

    @Override
    public Result update(AtricleModel atricleModel) {
        Boolean result = this.updateById(atricleModel);
        if (result) {
            return Result.success();
        }
        return Result.failed("更新失败");
    }

    @Override
    public Result delete(Long atricleId) {
        Boolean result = this.removeById(atricleId);
        return Result.judge(result);

    }

    private QueryWrapper<AtricleModel> queryWrapper(AtricleModel atricleModel) {
        QueryWrapper<AtricleModel> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(atricleModel.getArticleNumber())) {
            queryWrapper.like("article_number", atricleModel.getArticleNumber());
        }
        if (atricleModel.getBrandId() != null) {
            queryWrapper.eq("brand_id", atricleModel.getBrandId());
        }
        if (StringUtils.isNotEmpty(atricleModel.getSize())) {
            queryWrapper.eq("size", atricleModel.getSize());
        }
        if (StringUtils.isNotEmpty(atricleModel.getBarCode())) {
            queryWrapper.eq("bar_code", atricleModel.getBarCode());
        }

        /*
           写搜索条件
        */
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }

}
